import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import { useState, useRef } from "react";
/**
 * 超级锁钩子。未运行完毕锁。n毫秒运行一次锁。运行成功n毫秒后才能运行锁。
 *
 * @param setLoading
 * @param fun
 */ export default function useSuperLock(fun) {
    var delay = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 100;
    var _useState = _sliced_to_array(useState(false), 2), lock = _useState[0], setLock = _useState[1];
    var lastDate = useRef();
    var _this = this;
    return [
        /*#__PURE__*/ _async_to_generator(function() {
            var _len, args, _key, nowDate, returnValue, error;
            var _arguments = arguments;
            return _ts_generator(this, function(_state) {
                switch(_state.label){
                    case 0:
                        for(_len = _arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
                            args[_key] = _arguments[_key];
                        }
                        if (lock) {
                            return [
                                2
                            ];
                        }
                        nowDate = new Date();
                        if (lastDate.current && nowDate.getTime() - lastDate.current.getTime() <= delay) {
                            return [
                                2
                            ];
                        }
                        lastDate.current = nowDate;
                        setLock(true);
                        _state.label = 1;
                    case 1:
                        _state.trys.push([
                            1,
                            3,
                            ,
                            4
                        ]);
                        return [
                            4,
                            fun.apply(_this, args)
                        ];
                    case 2:
                        returnValue = _state.sent();
                        return [
                            3,
                            4
                        ];
                    case 3:
                        error = _state.sent();
                        setLock(false);
                        throw error;
                    case 4:
                        setTimeout(function() {
                            setLock(false);
                        }, delay);
                        return [
                            2,
                            returnValue
                        ];
                }
            });
        }),
        lock
    ];
}

 //# sourceMappingURL=useSuperLock.js.map